
#######################################
############# Figure 4 ################
#######################################

library(dplyr)
library(ggplot2)
library(readr)


# load data
df <- read_csv("Data/covidmentions.csv")

df |> 
  filter(date >= "2020-03-01", date <= "2020-09-30") |> 
  mutate(date_custom = lubridate::floor_date(date, "month")) |> 
  select(turnover, date_custom, covid) |> 
  mutate(turnover_str = if_else(turnover == 1, "b) turnover", "a) no turnover")) |> 
  group_by(turnover_str, date_custom) |> 
  summarize(n = sum(covid))  |> 
  ggplot(aes(x = date_custom, y = n)) +
  geom_col(fill = "lightgrey", color = "black") +
  geom_text(
    aes(
      x = as.Date("2020-08-15"), 
      y = 13, 
      label = turnover_str
    ), 
    check_overlap = TRUE
  ) +
  scale_x_date(
    date_breaks = "1 month", 
    date_labels = "%b %Y",
    limits = c(as.Date("2020-02-15"), as.Date("2020-09-30"))
  ) +
  scale_y_continuous(limits = c(0, 20)) +
  theme_classic() +
  theme(
    axis.text = element_text(color = "black"),
    #    axis.text.x = element_text(angle = 45, hjust = 1),
    strip.background = element_blank(),
    strip.text = element_blank()
  ) +
  labs(x = "Date", y = "Number of documents mentioning COVID (Monthly)") +
  facet_wrap(~ turnover_str, ncol = 1, scales = "free")


ggsave(
  filename = "Figures/fig4.eps", 
  plot = last_plot(), device = "eps", 
  width = 14.11, height = 10.86, units = "cm"
)
